from general_utilities import *
from map import *
from entities import *
from math import pi, tan, cos

# Has been gutted - now only contains Rays setup - actual los check is in gamestate.py

N_ORDINARY_RAYS = 360
EXTRA_RAY_MIN = 3
EXTRA_RAY_MAX = 12
N_EXTRA_RAYS = 8 * (EXTRA_RAY_MAX - EXTRA_RAY_MIN + 1)
N_RAYS = N_ORDINARY_RAYS + N_EXTRA_RAYS

class Rays:
  # info needed for the LOS method
  # just create once, it goes in d.rays
  def __init__(self, MAX_MAP_DIM):
    r = {}  # keyed by down, then right; entry is [ray-number]
    for down in range(-MAX_MAP_DIM+1,MAX_MAP_DIM):
      r[down] = {}
      for right in range(-MAX_MAP_DIM+1,MAX_MAP_DIM):
        r[down][right] = []
    seq = {} # keyed by ray-number, entry is [Pos]   
    for ray in range(N_ORDINARY_RAYS):
      angle = (ray + 0.5) * 360.0 / N_ORDINARY_RAYS
      tiles = [] # use Bresenham-like algorithm to generate [(down1,right1),...]
      if angle == 0:
        for i in one_to_x(MAX_MAP_DIM-1):
          tiles.append([-i,0])
      elif angle == 180:
        for i in one_to_x(MAX_MAP_DIM-1):
          tiles.append([i,0])
      else:
        steep = tan(float(angle + 90) * pi / 180)
        error = float(0)
        if angle > 180:
          right_inc = -1
        else:
          right_inc = 1
        if angle > 90 and angle < 270:
          down_inc = 1
        else:
          down_inc = -1  
        right = 0
        down = 0
        if abs(steep) < 1:
          for i in one_to_x(MAX_MAP_DIM-1):
            right = right + right_inc
            error = error + abs(steep)
            if error > 0.5:
              down = down + down_inc
              error = error - 1
            if down >= -MAX_MAP_DIM+1 and down <= MAX_MAP_DIM-1:
              tiles.append((down,right))
        else:
          steep = 1.0 / steep
          for i in one_to_x(MAX_MAP_DIM-1):
            down = down + down_inc
            error = error + abs(steep)
            if error > 0.5:
              right = right + right_inc
              error = error - 1
            if right >= -MAX_MAP_DIM+1 and right <= MAX_MAP_DIM-1:
              tiles.append((down,right))      
      seq[ray] = tiles
      for tile in tiles:
        r[tile[0]][tile[1]].append(ray)
    #extra_ray_down_inc = [-1, -1, 0, 0, 1, 1, 0, 0]
    #extra_ray_right_inc = [0, 0, -1, -1, 0, 0, 1, 1]
    #extra_ray_end_down_inc = [0, 0, 1, -1, 0, 0, 1, -1]
    #extra_ray_end_right_inc = [1, -1, 0, 0, 1, -1, 0, 0]
    #ray_counter = 0
    #for i in range(8):
      #for end_at in range(EXTRA_RAY_MIN,EXTRA_RAY_MAX):
        #ray_counter = ray_counter + 1
        #tiles = []
        #down = 0
        #right = 0
        #for j in range(end_at):
          #down = down + extra_ray_down_inc[i]       
          #right = right + extra_ray_right_inc[i]
          #tiles.append((down,right))
        #for j in range(2):
          #down = down + extra_ray_down_inc[i] + extra_ray_end_down_inc[i]   
          #right = right + extra_ray_right_inc[i] + extra_ray_end_right_inc[i]
          #tiles.append((down,right))
        #seq[ray_counter + N_ORDINARY_RAYS] = tiles
        #for tile in tiles:
          #r[tile[0]][tile[1]].append(ray_counter + N_ORDINARY_RAYS)
    self.r = r  
    self.seq = seq

# do some psyco bindings 
psyco.bind(Rays.__init__)